﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Linq.Expressions;

namespace TEMS.Controls
{
    class General
    {/// <summary>
        /// 获取属性需要显示的名称
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static string GetDisplayName(PropertyInfo p)
        {
            string displayName = "";

            //DisplayNameAttribute attr = p.GetCustomAttribute<DisplayNameAttribute>();
            //if (attr != null)
            //{
            //    displayName = attr.DisplayName;
            //}
            //else
            //{
            //    displayName = p.Name;
            //}
            return displayName;
        }

        /// <summary>
        /// 获取指定属性信息（非String类型存在装箱与拆箱）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="select"></param>
        /// <returns></returns>
        public static PropertyInfo GetPropertyInfo<T>(Expression<Func<T, dynamic>> select)
        {
            var body = select.Body;
            if (body.NodeType == ExpressionType.Convert)
            {
                var o = (body as UnaryExpression).Operand;
                return (o as MemberExpression).Member as PropertyInfo;
            }
            else if (body.NodeType == ExpressionType.MemberAccess)
            {
                return (body as MemberExpression).Member as PropertyInfo;
            }
            return null;
        }
    }
}
